package com.mnemonic.support.swing;

import java.awt.SystemTray;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 * 界面
 * 
 * @author lifeng
 * @date 2021/05/13
 */
public enum Display {

    INSTANCE;

    private static JFrame root;
    private static String title;
    private static SystemTray systemTray;

    public static JFrame getRoot() {
        return root;
    }

    public static void setRoot(JFrame root) {
        Display.root = root;
    }

    public static String getTitle() {
        return title;
    }

    public static void setTitle(final String title) {
        Display.title = title;
    }

    public static SystemTray getSystemTray() {
        return systemTray;
    }

    static void setSystemTray(SystemTray systemTray) {
        Display.systemTray = systemTray;
    }

    /**
     * 加载资源
     * 
     * @param path
     * @return
     */
    public static java.net.URL load(String path) {
        return Display.class.getResource(path);
    }

    /**
     * 加载资源
     * 
     * @param path
     * @return
     */
    public static java.net.URL load(Class<?> clazz, String path) {
        return clazz.getResource(path);
    }

    /**
     * 如果当前线程属于 UI 线程，则执行 runnable，否则调用 SwingUtilities.invokeLater() 来执行 runnable。 这样能保证 runnable 是在 UI 线程上执行。
     * 
     * @param runnable
     */
    public static void runUI(Runnable runnable) {
        if (SwingUtilities.isEventDispatchThread()) {
            runnable.run();
        } else {
            SwingUtilities.invokeLater(runnable);
        }
    }
}